/**
 * 
 */
package br.edu.catolica.sadbc.action;

import java.util.ArrayList;
import java.util.List;

import javax.faces.event.ActionEvent;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.datamodel.DataModel;

import br.edu.catolica.sadbc.core.action.AbstractAction;
import br.edu.catolica.sadbc.core.exception.BusinessObjectException;
import br.edu.catolica.sadbc.core.exception.ValidateBusinessRulesException;
import br.edu.catolica.sadbc.entity.Saida;
import br.edu.catolica.sadbc.entity.Usuario;
import br.edu.catolica.sadbc.entity.enums.SimNao;
import br.edu.catolica.sadbc.entity.filter.SaidaSearchFilter;
import br.edu.catolica.sadbc.manager.SaidaManager;

/**
 * @author robert.resende
 *
 */
@Name(value = "saidaAction")
public class SaidaAction extends AbstractAction<Saida> {
	
	@In(required = false)
	@Out(required = false)
	private Saida saida;
	
	@In(required = false)
	private SaidaSearchFilter saidaSearchFilter;
	
	@In(required = false)
	private SaidaManager saidaManager;
	
	@In(required = false)
	private Usuario usuarioSession;
	
	@DataModel
	private List<Saida> saidas = new ArrayList<Saida>();

	private List<Saida> saidasDocente = new ArrayList<Saida>();

	private Boolean exibeDataTable = Boolean.FALSE;
	
	private Boolean exibeCampos = Boolean.TRUE;
	
	public void init() {
		this.saida = null;
		this.saidaSearchFilter = null;
		this.saidas = new ArrayList<Saida>();
		this.exibeCampos = Boolean.TRUE;
		this.exibeDataTable = Boolean.FALSE;
	}
	
	public List<Saida> visualizarSaidasAgendadas() {
		this.saidasDocente = this.saidaManager.buscarSaidasAgendadas(this.usuarioSession);
		if (this.saidasDocente.isEmpty()) {
			addMessageInfo("O docente não possui nenhum saida agendada!");
		}
		return this.saidasDocente;
	}
	
	public void escondeCampos(ActionEvent event) {
		log.info("Passou aqui...1");
		if (this.saidaSearchFilter.getDocente() != null && this.saidaSearchFilter.getDocente().getId() != null) {
			exibeCampos = Boolean.FALSE;
		} else {
			exibeCampos = Boolean.TRUE;
		}
	}
	
	public List<Saida> pesquisar() {
		if (validarCamposPesquisa(this.saidaSearchFilter)) {
			this.saidaSearchFilter.setAtiva(SimNao.SIM.getValue());
			this.saidas = this.saidaManager.pesquisar(this.saidaSearchFilter);
			if (!this.saidas.isEmpty()) {
				this.exibeDataTable = Boolean.TRUE;
			} else {
				this.saidas.clear();
				addMessageInfo("Nenhum resultado encontrado.");
			}
		}
		return saidas;
	}
	
	public String salvar() {
		try {
			String retorno = this.saidaManager.salvar(this.saida);
			addMessageInfo("Operação realizada com sucesso!");
			return retorno;
		} catch (ValidateBusinessRulesException vbe) {
			vbe.printStackTrace();
			addMessageErro(vbe.getLocalizedMessage());
			return RESULT_FAIL;
		} catch (BusinessObjectException boe) {
			boe.printStackTrace();
			addMessageErro(boe.getLocalizedMessage());
			return RESULT_FAIL;
		}
	}
	
	public String atualizar() {
		try {
			String retorno = this.saidaManager.update(this.saida);
			addMessageInfo("Operação realizada com sucesso!");
			return retorno;
		} catch (ValidateBusinessRulesException vbe) {
			vbe.printStackTrace();
			addMessageErro(vbe.getLocalizedMessage());
			return RESULT_FAIL;
		} catch (BusinessObjectException boe) {
			boe.printStackTrace();
			addMessageErro(boe.getLocalizedMessage());
			return RESULT_FAIL;
		}
	}
	
	public String excluir(Saida saida) {
		try {
			String retorno = this.saidaManager.excluir(saida);
			addMessageInfo("Operação realizada com sucesso!");
			return retorno;
		} catch (ValidateBusinessRulesException vbe) {
			vbe.printStackTrace();
			addMessageErro(vbe.getLocalizedMessage());
			return RESULT_FAIL;
		} catch (BusinessObjectException boe) {
			boe.printStackTrace();
			addMessageErro(boe.getLocalizedMessage());
			return RESULT_FAIL;
		}
	}
	
	public String editar(Saida saida) {
		this.saida = saida;
		return RESULT_EDIT;
	}
	
	public String detalhar(Saida saida) {
		this.saida = saida;
		return RESULT_DETAIL;
	}
	
	private boolean validarCamposPesquisa(SaidaSearchFilter saidaSearchFilter) {
		return Boolean.TRUE;
	}

	public Boolean getExibeDataTable() {
		return exibeDataTable;
	}

	public void setExibeDataTable(Boolean exibeDataTable) {
		this.exibeDataTable = exibeDataTable;
	}

	public Boolean getExibeCampos() {
		return exibeCampos;
	}

	public void setExibeCampos(Boolean exibeCampos) {
		this.exibeCampos = exibeCampos;
	}

	public List<Saida> getSaidasDocente() {
		return saidasDocente;
	}

	public void setSaidasDocente(List<Saida> saidasDocente) {
		this.saidasDocente = saidasDocente;
	}

}
